//
//  FirstTwoBannerCell.m
//  DaJiHui
//
//  Created by Mac on 2017/9/27.
//  Copyright © 2017年 Hao. All rights reserved.
//

#import "FirstTwoBannerCell.h"
#import "FirstTwoBannerModel.h"
#import "UIImageView+WebCache.h"

@interface FirstTwoBannerCell()<UIScrollViewDelegate>

@property (nonatomic, strong) UIImageView *logoImg;         //当前显示图片
@property (nonatomic, strong) UILabel *storeLabel;
@property (nonatomic, strong) UILabel *addressLabel;
@property (nonatomic, strong) UIButton *goodsBtn;
@end

@implementation FirstTwoBannerCell


- (void)layoutSubviews
{
    [super layoutSubviews];
    [self.logoImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(@(0));
        make.width.equalTo(self);
        make.height.equalTo(@(150));
    }];
    
    [self.storeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(15);
        make.top.equalTo(self.logoImg.mas_bottom).offset(15);
        [self.storeLabel sizeToFit];
    }];
    
    [self.addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(15);
        make.top.equalTo(self.storeLabel.mas_bottom).offset(15);
        [self.addressLabel sizeToFit];
    }];
    
    [self.goodsBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(30);
        make.right.equalTo(self).offset(-30);
        make.width.equalTo(@(ZTY_SCREEN_WIDTH - 60));
        make.top.equalTo(self.addressLabel.mas_bottom).offset(15);
        make.height.equalTo(@(30));
    }];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initData];
        [self initContentView];
    }
    return self;
}

- (void)initData
{
    
}

- (void)initContentView
{
    [self addSubview:self.logoImg];
    [self addSubview:self.storeLabel];
    [self addSubview:self.addressLabel];
    [self addSubview:self.goodsBtn];
}

- (UIImageView *)logoImg
{
    if (!_logoImg) {
        _logoImg = [[UIImageView alloc] init];
        _logoImg.contentMode = UIViewContentModeScaleAspectFill;
        _logoImg.backgroundColor = [UIColor redColor];
        _logoImg.layer.masksToBounds = YES;
    }
    return _logoImg;
}

- (UILabel *)storeLabel
{
    if (!_storeLabel) {
        _storeLabel = [[UILabel alloc] init];
        _storeLabel.textColor = [UIColor blackColor];
        _storeLabel.textAlignment= NSTextAlignmentLeft;
        _storeLabel.font = [UIFont systemFontOfSize:15];
    }
    return _storeLabel;
}

- (UILabel *)addressLabel
{
    if (!_addressLabel) {
        _addressLabel = [[UILabel alloc] init];
        _storeLabel.textColor = [UIColor blackColor];
        _storeLabel.textAlignment = NSTextAlignmentLeft;
        _storeLabel.font = [UIFont systemFontOfSize:15];
    }
    return _addressLabel;
}

- (UIButton *)goodsBtn
{
    if (!_goodsBtn) {
        _goodsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_goodsBtn setTitle:@"店家商品" forState:UIControlStateNormal];
        [_goodsBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_goodsBtn setBackgroundColor:[UIColor whiteColor]];
        _goodsBtn.layer.borderWidth = 1;
        _goodsBtn.layer.borderColor = [UIColor blackColor].CGColor;
        [_goodsBtn addTarget:self action:@selector(jumpGoos) forControlEvents:UIControlEventTouchUpInside];
    }
    return _goodsBtn;
}

- (void)setModel:(FirstTwoBannerModel *)model
{
    _model = model;
    NSString *url = [self getPicUrl:model.logo];
    if (url.length) {
        [self.logoImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.chaojihui123.com%@",[url substringFromIndex:8]]]];
    }
    self.storeLabel.text = model.storename;
    self.addressLabel.text = [NSString stringWithFormat:@"%@%@%@",model.location_c,model.location_a,model.location_d];
    [self layoutIfNeeded];
}


- (void)jumpGoos
{
    if (self.JumpGoosHandler) {
        self.JumpGoosHandler();
    }
}

- (NSString *)getPicUrl:(id)pic
{
    if (pic == NULL || pic == nil) {
        return @"";
    }else if(pic == [NSNull class]){
        return @"";
    }else if (pic == [NSNull null]){
        return @"";
    }else if ([pic isEqualToString:@"null"]){
        return @"";
    }else{
        return pic;
    }
}

@end

